<template>
    <!-- 出库订单选择客户弹窗 -->
    <div class="dialog-box">
        <el-dialog v-model="open" title="选择客户" width="800" close-on-press-escape="false">
            <div class="dialog-content">
                <!-- 左侧客户级别列表 -->
                <div class="customer-level">
                    <div class="level-header">
                        <span>客户等级</span>
                        <el-icon>
                            <Search />
                        </el-icon>
                    </div>
                    <div class="level-list">
                        <div v-for="item in customerLevel" :key="item.id" class="level-item"
                            :class="{ active: selectedLevel === item.id }" @click="handleLevelSelect(item.id)">
                            {{ item.name }}
                        </div>
                    </div>
                </div>

                <!-- 右侧客户列表 -->
                <div class="customer-list">
                    <div class="table-body">
                        <el-table :data="tableData" border style="width: 100%"
                            :header-cell-style="{ background: '#f5f7fa', color: '#606266' }">
                            <!-- 选择客户 -->
                            <el-table-column width="50">
                                <template #default="scope">
                                    <el-radio v-model="selectedRowId" :label="scope.row.id"
                                        @change="handleRadioChange(scope.row)">
                                        <span></span>
                                    </el-radio>
                                </template>
                            </el-table-column>
                            <el-table-column prop="name" label="客户名称" />
                            <el-table-column prop="phone" label="联系电话" />
                            <el-table-column prop="region" label="地区" >
                                <template #default="scope">
                                    <span>{{ scope.row.province }} {{ scope.row.city }} {{ scope.row.area }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="address" label="详细地址" />
                        </el-table>
                    </div>
                </div>
            </div>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="open = false">取消</el-button>
                    <el-button type="primary" @click="submit">确认</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import api from "@/api";
import { ref, defineEmits } from "vue";
import { Search } from "@element-plus/icons-vue";
import type { ElTable } from "element-plus";
// 定义的客户信息ts
interface Customer {
    id: number;
    name: string;
    phone: string;
    region: string;
    address: string;
    province?: string; // 省份
    city?: string; // 城市
    area?: string; // 区域
}

const open = ref(false);// 弹窗控制器
const selectedRowId = ref<number>();// 选择的客户id
const selectedRow = ref<Customer>();// 选择的客户信息
const selectedLevel = ref(0);// 选择的客户等级id

const customerLevel = ref<any[]>([]);

// 获取客户等级
const getCustomerLevel = async () => {
    const res = await api.publicFun.getLevelList();
    if (res.code === 200) {
        customerLevel.value = res.data;
    }
};

const tableData = ref<Customer[]>([
]);

// 获取客户列表
const getCustomerLists = async () => {
    const res = await api.publicFun.getCustomerList({
        level_id: selectedLevel.value
    });
    if (res.code === 200) {
        tableData.value = res.data;
    }
};

// 选择客户等级
const handleLevelSelect = (level: number) => {
    selectedLevel.value = level;
    getCustomerLists();
};
// 选择的客户信息
const handleRadioChange = (row: Customer) => {
    selectedRow.value = row;
    console.log(selectedRow.value);
};

const openLoading = () => {
    // 重置
    selectedRow.value = undefined;
    selectedLevel.value = 0;
    tableData.value = [];
    // 打开弹窗
    open.value = true;
    // 获取客户列表
    getCustomerLists();
    // 获取客户等级
    getCustomerLevel();
};

const emit = defineEmits(['selectCustomer']);
// 确认然后将数据传递给新增出库单
const submit = () => {
    if (selectedRow.value) {
        const customerData = {
            id: selectedRow.value.id,
            name: selectedRow.value.name,
            phone: selectedRow.value.phone,
            region: selectedRow.value.province && selectedRow.value.city && selectedRow.value.area 
                ? `${selectedRow.value.province} ${selectedRow.value.city} ${selectedRow.value.area}`.trim()
                : selectedRow.value.region || '', // 如果没有省市区字段，使用region字段
            address: selectedRow.value.address
        };
        
        console.log('选中的客户数据:', customerData);
        
        emit('selectCustomer', customerData);
        open.value = false;
    } else {
        console.log('请先选择一个客户');
    }
};

defineExpose({
    openLoading
});
</script>

<style lang="scss" scoped>
.dialog-content {
    display: flex;
    height: 400px;
    gap: 10px;
}

.customer-level {
    width: 200px;
    border: 1px solid #e4e7ed;

    .level-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 12px 16px;
        border-bottom: 1px solid #e4e7ed;
        background-color: #fff;

        .el-icon {
            cursor: pointer;
            color: #909399;
        }
    }

    .level-list {
        .level-item {
            padding: 12px 16px;
            cursor: pointer;
            transition: all 0.3s;

            &:hover,
            &.active {
                background-color: #f0f2f5;
                color: #626aef;
            }
        }
    }
}

.customer-list {
    flex: 1;
    border: 1px solid #e4e7ed;

    .table-header {
        background-color: #f5f7fa;
        border-bottom: 1px solid #e4e7ed;
    }
}

:deep(.el-dialog) {
    --el-dialog-padding-primary: 0;

    .el-dialog__header {
        margin: 0;
        padding: 15px 20px;
        border-bottom: 1px solid #e4e7ed;

        .el-dialog__title {
            font-size: 16px;
            font-weight: 500;
        }
    }

    .el-dialog__body {
        padding: 20px;
    }

    .el-dialog__footer {
        border-top: 1px solid #e4e7ed;
        padding: 15px 20px;
    }
}

:deep(.el-table) {
    .el-table__header {
        th {
            background-color: #f5f7fa;
            color: #606266;
            font-weight: 500;
        }
    }

    .el-radio {
        margin-right: 0;
    }
}

.dialog-footer {
    .el-button+.el-button {
        margin-left: 12px;
    }
}
</style>